/**
 * TeamListView and TeamListController are tested
 * @author maulin
 */
dojo.provide("medryx.widgets._TeamList.tests.TeamListTest");
dojo.require("medryx.widgets.TeamList");
dojo.require("doh.TestCase")
dojo.require("medryx.model.Team");
dojo.require("medryx.model.Pager");
dojo.require("medryx.model.TeamPager");
dojo.require("doh.runner");
dojo.require("medryx.orm.EntityManager")

dojo.declare("TeamListTest.TeamList", [medryx.widgets._TeamList.TeamListView], {
	postCreate:function() {
		this.controller = new medryx.widgets._TeamList.TeamListController(this);
        this.inherited(arguments);		
	},
    loadTeams:function() {
        this.controller.loadTeams(arguments[0]);
    }
})

if(dojo.isBrowser){
//    doh.registerUrl("medryx.widgets.tests.TeamListTest", dojo.moduleUrl("medryx.widgets.tests", "TeamListTest.html"), 15000);
}

dojo.declare("TeamListTest.fixture", doh.TestCase, {
    setUp:function() {
        this.teamList = new TeamListTest.TeamList();
		this.lazy = {
			load:function() {
				console.debug("in lazy loader", arguments);
			}
		}
		this.ef = new medryx.orm.EntityManager(this.lazy);
		this.ef.registerClass("medryx.model.Team");
		this.ef.registerClass("medryx.model.TeamPager");
		this.ef.registerClass("medryx.model.Pager");
		this.team1 = this.ef.buildEntity("medryx.model.Team", {
            id: 15,
            name: "Orange Team",
            color: "orange",
            abbreviation: "O",
            category: "Day Teams",
            categorySortIndex: "1",
            pagers: [{
                type: "Attending",
                pager:{id: "1066",
                phoneNumber: "216-6338 x1066"}
            }]
        });
		this.team2 = this.ef.buildEntity("medryx.model.Team", {
            id: 22,
            name: "PMU C",
            abbreviation: "PMUC",
            category: "PMU Teams",
            categorySortIndex: "4",
            pagers: [{
                type: "Intern",
                pager: {id: "6141",
                phoneNumber: "216-6338 x6141"}
            }, {
                type: "Resident",
                pager: {id: "6095",
                phoneNumber: "216-6338 x6095"}
            }]
        });
    }
});

(function() {

var fixture = TeamListTest.fixture;

doh.register("medryx.widgets.tests.TeamListTest", [
    new fixture("testParseInSortNull", function() {
        var sort = teamList._parseInSort(null);
        assertEqual(0, sort.length);
        assertTrue(dojo.isArray(sort));
    }),
	 new fixture("testParseInSortEmptyString", function() {
        var sort = teamList._parseInSort("");
        assertEqual(0, sort.length);
        assertTrue(dojo.isArray(sort));
    }),
	 new fixture("testParseInSortEmptyArray", function() {
        var sort = teamList._parseInSort([]);
        assertEqual(0, sort.length);
        assertTrue(dojo.isArray(sort));
    }),
	 new fixture("testParseInSortSingleString", function() {
        var sort = teamList._parseInSort("someField");
        assertEqual(1, sort.length);
        assertTrue(dojo.isArray(sort));
		assertEqual("someField", sort[0].field);
    }),
	 new fixture("testParseInSortSingleItemArray", function() {
        var sort = teamList._parseInSort(["someField"]);
        assertEqual(1, sort.length);
        assertTrue(dojo.isArray(sort));
        assertEqual("someField", sort[0].field);
    }),
	 new fixture("testParseInSortMultipleString", function() {
        var sort = teamList._parseInSort("someField1,    someField2,someField3 ,    someField4");
        assertEqual(4, sort.length);
        assertTrue(dojo.isArray(sort));
        assertEqual("someField1", sort[0].field);
		assertEqual("someField2", sort[1].field);
		assertEqual("someField3", sort[2].field);
		assertEqual("someField4", sort[3].field);
    }),
	 new fixture("testParseInSortMultipleArray", function() {
        var sort = teamList._parseInSort(["someField1", "someField2", "someField3", "someField4"]);
        assertEqual(4, sort.length);
        assertTrue(dojo.isArray(sort));
        assertEqual("someField1", sort[0].field);
        assertEqual("someField2", sort[1].field);
        assertEqual("someField3", sort[2].field);
        assertEqual("someField4", sort[3].field);
    }),
	 new fixture("testParseInSortMultipleArrayOfSortObjects", function() {
        var sort = teamList._parseInSort([{
			field: "someField1"
		}, {
			field: "someField2"
		}, {
			field: "someField3"
		}, {
			field: "someField4"
		}]);
        assertEqual(4, sort.length);
        assertTrue(dojo.isArray(sort));
        assertEqual("someField1", sort[0].field);
        assertEqual("someField2", sort[1].field);
        assertEqual("someField3", sort[2].field);
        assertEqual("someField4", sort[3].field);
    }),
    new fixture("testActualSortNull", function() {
        var actualSort = teamList.validateSort(null);
        assertEqual(3, actualSort.length);
        assertEqual("categorySortIndex", actualSort[0].field);
    }),
     new fixture("testActualSortEmpty", function() {
        var actualSort = teamList.validateSort([]);
        assertEqual(3, actualSort.length);
        assertEqual("categorySortIndex", actualSort[0].field);
    }),
     new fixture("testActualSortNotCategoryString", function() {
        var actualSort = teamList.validateSort("someField");
        assertEqual(4, actualSort.length);
        assertEqual("categorySortIndex", actualSort[0].field);
        assertEqual("someField", actualSort[3].field);
    }),
     new fixture("testActualSortMultipleNoCategoryArray", function() {
        var actualSort = teamList.validateSort(["someField1", "someField2", "someField3", "someField4"]);
        assertEqual(7, actualSort.length);
        assertEqual("categorySortIndex", actualSort[0].field);
        assertEqual("someField1", actualSort[3].field);
        assertEqual("someField2", actualSort[4].field);
        assertEqual("someField3", actualSort[5].field);
        assertEqual("someField4", actualSort[6].field);
    }),
	 new fixture("testOnMouseOverRow", function() {
        var tr = document.createElement("tr");
		teamList.onMouseOverRow(tr);
		assertTrue(dojo.hasClass(tr, "hover"));
    }),
	new fixture("testOnMouseOutRow", function() {
        var tr = document.createElement("tr");
		dojo.addClass(tr, "hover");
        teamList.onMouseOutRow(tr);
        assertFalse(dojo.hasClass(tr, "hover"));
    }),
	new fixture("testMakeCategoryHeader", function() {
        var tr = teamList.makeCategoryHeader("testCategory");
        assertTrue(tr != null);
		assertTrue(dojo.hasClass(tr, "teamListCategoryRow"));
		var children = dojo.query("th", tr);
		assertEqual(1, children.length);
		assertEqual("testCategory", children[0].innerHTML);
		assertEqual(2, children[0].colSpan);
    }),
	new fixture("testCreateTeamDetailsCell1Pager", function() {
		
        var td = teamList.createTeamDetailsCell(team1);
		assertEqual("216-6338 x1066", td.innerHTML);
		assertTrue(dojo.hasClass(td, "teamListDetailCell"));
	}),
	new fixture("testCreateTeamDetailsCell2Pager", function() {
        var td = teamList.createTeamDetailsCell(team2);
        assertEqual("216-6338 x6141 (Intern)<br>216-6338 x6095 (Resident)", td.innerHTML);
        assertTrue(dojo.hasClass(td, "teamListDetailCell"));
    }),
	new fixture("testMakeTeamRow", function() {
        var tr = teamList.makeTeamRow(team1, 1);
		
		var onMouseOverCalled = false;
		teamList.onMouseOverRow = function(row) {
			assertEqual(tr, row);
			onMouseOverCalled = true;
		}
		var event = document.createEvent("MouseEvents");
		event.initMouseEvent('mouseover', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
		tr.dispatchEvent(event);
		
		assertTrue(onMouseOverCalled);
		
		
		var onMouseOutCalled = false;
        teamList.onMouseOutRow = function(row) {
            assertEqual(tr, row);
            onMouseOutCalled = true;
        }
        event = document.createEvent("MouseEvents");
        event.initMouseEvent('mouseout', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
		tr.dispatchEvent(event);
		assertTrue(onMouseOutCalled);
		
		assertTrue(dojo.hasClass(tr, "odd"));
		
		assertEqual(2, dojo.query("td", tr).length);
		var detailCell = dojo.query("td:last-child", tr)[0];
		assertTrue(dojo.hasClass(detailCell, "teamListDetailCell"));
		
		assertEqual("Orange Team", dojo.query("td:first-child a", tr)[0].innerHTML);
		
		var teamClicked = false;
		var inteam = team1;
		teamList.onTeamClicked = function(team) {
            assertEqual(team, inteam);
            teamClicked = true;
        }
		
		event = document.createEvent("MouseEvents");
        event.initMouseEvent('click', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
		var dispatched = tr.dispatchEvent(event);
		assertTrue(dispatched);
		assertTrue(teamClicked);
    }),
	new fixture("testDisplayTeams", function() {
        var teams = [team1, team1, team2, team2];
		var tbody = document.createElement("tbody");
		var headerCell = document.createElement("th");
		var footerCell = document.createElement("td");
		
		teamList.headerContent = "header";
		teamList.footerContent = "footer";
		
		teamList.displayTeams(tbody, headerCell, footerCell, teams);
		
		var rows = dojo.query("tr", tbody);
		assertEqual(6, rows.length);
		assertEqual("Day Teams", rows[0].firstChild.innerHTML);
		assertEqual("Orange Team", rows[1].firstChild.firstChild.innerHTML);
		assertEqual("Orange Team", rows[2].firstChild.firstChild.innerHTML);
		
		assertEqual("PMU Teams", rows[3].firstChild.innerHTML);
		assertEqual("PMU C", rows[4].firstChild.firstChild.innerHTML);
		assertEqual("PMU C", rows[5].firstChild.firstChild.innerHTML);
		
		assertEqual("Teams", headerCell.innerHTML);
		assertEqual("", footerCell.innerHTML);
    })
]);
})();